package com.jilintongyi.jlcrtvu.entity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Where;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity // 实体映射
@Table(name = "sys_user") // 表名
@org.hibernate.annotations.Table(appliesTo = "sys_user", comment = "系统用户表") // 表注释
public class SysUser extends BaseEntity implements UserDetails {
	@ManyToMany(fetch = FetchType.EAGER) // 急加载
	@JoinTable(name = "SysUserRole", joinColumns = @JoinColumn(name = "userId"), inverseJoinColumns = @JoinColumn(name = "roleId")) // 关联表名
	@Where(clause = "deleted=false")
	private List<SysRole> roles;

	@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
	@Where(clause = "deleted=false")
	@JsonIgnore // json序列化忽略
	private List<SysLog> logs;

	@Column(unique = true, nullable = false, columnDefinition = "varchar(255) comment '用户名'") // 唯一不能为空
	private String username;

	@Column(nullable = false, columnDefinition = "varchar(255) comment '密码'")
	private String password;

	@Column(nullable = false, columnDefinition = "bit(1) default 1 comment '账号未过期'")
	private Boolean accountNonExpired = true;

	@Column(nullable = false, columnDefinition = "bit(1) default 1 comment '账号未锁定'")
	private Boolean accountNonLocked = true;

	@Column(nullable = false, columnDefinition = "bit(1) default 1 comment '凭证未过期'")
	private Boolean credentialsNonExpired = true;

	@Column(nullable = false, columnDefinition = "bit(1) default 0 comment '启用'")
	private Boolean enabled = false;

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		List<SysAuthority> authorities = new ArrayList<>();
		if (null != roles) {
			for (SysRole role : roles) {
				authorities.addAll(role.getAuthorities());
			}
		}
		return authorities;
	}

	@Override
	public String getPassword() {
		return this.password;
	}

	@Override
	public String getUsername() {
		return this.username;
	}

	@Override
	public boolean isAccountNonExpired() {
		return this.accountNonExpired;
	}

	@Override
	public boolean isAccountNonLocked() {
		return this.accountNonLocked;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return this.credentialsNonExpired;
	}

	@Override
	public boolean isEnabled() {
		return this.enabled;
	}

	public List<SysRole> getRoles() {
		return roles;
	}

	public void setRoles(List<SysRole> roles) {
		this.roles = roles;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Boolean getAccountNonExpired() {
		return accountNonExpired;
	}

	public void setAccountNonExpired(Boolean accountNonExpired) {
		this.accountNonExpired = accountNonExpired;
	}

	public Boolean getAccountNonLocked() {
		return accountNonLocked;
	}

	public void setAccountNonLocked(Boolean accountNonLocked) {
		this.accountNonLocked = accountNonLocked;
	}

	public Boolean getCredentialsNonExpired() {
		return credentialsNonExpired;
	}

	public void setCredentialsNonExpired(Boolean credentialsNonExpired) {
		this.credentialsNonExpired = credentialsNonExpired;
	}

	public Boolean getEnabled() {
		return enabled;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}
}
